set -e

. /etc/os-release

arch=$(dpkg --print-architecture)
ros2_list="/etc/apt/sources.list.d/ros2-snapshots.list"

if [[ -e ${ros2_list} ]]; then
	ros_key="/usr/share/keyrings/ros-archive-keyring.gpg"
	# curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o ${ros_key}
	python3 -c "from urllib.request import urlopen; open('${ros_key}', 'wb').write(urlopen('https://raw.githubusercontent.com/ros/rosdistro/master/ros.key').read())"
	echo "deb [arch=${arch} signed-by=${ros_key}] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu ${VERSION_CODENAME} main" >"${ros2_list}"
fi

cat << EOF >/etc/apt/sources.list
deb [arch=${arch}] https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
deb [arch=${arch}] https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
deb [arch=${arch}] https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
deb [arch=${arch}] http://security.ubuntu.com/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
EOF
